<?php
class QueryDistrictHandler implements IHandler {
	/**
	 * <h2>日志对象</h2>
	 *
	 * @var Logger
	 */
	private $_logger;
	
	/**
	 * <h2>构造函数</h2>
	 * <p>初始化日志对象等内部属性</p>
	 */
	public function __construct() {
		Logger::configure ( ROOT_PATH . 'config/log_config.xml' );
		$this->_logger = Logger::getLogger ( __CLASS__ );
	}
	
	/**
	 * <h2>处理前端根据城市ID查询区县的请求</h2>
	 * <p>前端的query_string需要自定城市id(cid)参数，如果没有查询到则输出为空</p>
	 *
	 * @see IHandler::handle()
	 */
	public function handle() {
		$request = new Request ();
		try {
			if (! $request->isAjax ()) {
				throw new ExceptionEx ( 'Not Ajax Request' . 'class[' . __CLASS__ . '] line[' . __LINE__ . ']' );
			}
			$cid = new CINT ( $request->getGet ( 'cid' ) );
			if ($cid->getValue () == 0) {
				throw new ExceptionEx ( 'Invalid City ID' . 'class[' . __CLASS__ . '] line[' . __LINE__ . ']' );
			}
			require_once ROOT_PATH . 'models/DistrictModel.class.php';
			$dm = new DistrictModel ();
			$list = $dm->getDistrictByCityId ( $cid );
			$html = '';
			$first = true;
			foreach ( $list as $item ) {
				if ($first) {
					$html .= "<option value=\"{$item->id}\" selected=\"selected\">{$item->name}</option>\n";
					$first = false;
				} else {
					$html .= "<option value=\"{$item->id}\">{$item->name}</option>\n";
				}
			}
			header ( "Content-Type:text/plain;charset=utf-8" );
			echo $html;
		} catch ( ExceptionEx $e ) {
		}
	}
}